<template>
  <div id="detail">
    <detail-nav-bar class="detail-nav" @titleClick="titleClick" ref="navbar" />
    <div class="detail-scroll">
      <scroll class="content" ref="scroll" @scroll="scroll" :probeType="3">
        <detail-swiper :top-images="topImages" />
        <detail-base-info :goods="goods" />
        <detail-shop-info :shop="shop" />
        <detail-goods-info :detail-info="detailInfo" @imageLoad="imageLoad" />
        <detail-goods-param ref="params" :goodsParam="goodsParam" />
        <detail-comment-info ref="comment" :commentInfo="commentInfo" />
        <goods-list ref="recommend" :goods="recommend" />
      </scroll>
    </div>
    <detail-bottom-bar @addCart="addCart" />
    <back-top @click.native="backClick" v-show="backIsShow" class="backTop" />
    <!-- <toast :message="message" :isShow="isShow" /> -->
  </div>
</template>

<script>
// 导入组件
import DetailNavBar from "./childComps/DetailNavBar.vue";
import DetailSwiper from "./childComps/DetailSwiper.vue";
import DetailBaseInfo from "./childComps/DetailBaseInfo.vue";
import DetailShopInfo from "./childComps/DetailShopInfo.vue";
import DetailGoodsInfo from "./childComps/DetailGoodsInfo.vue";
import DetailGoodsParam from "./childComps/DetailGoodsParam.vue";
import DetailCommentInfo from "./childComps/DetailCommentInfo.vue";
import DetailBottomBar from "./childComps/DetailBottomBar";
import GoodsList from "components/content/goods/GoodsList.vue";

// 导入better scroll
import scroll from "components/common/scroll/Scroll.vue";

// 导入网络请求
import {
  getDetail,
  Goods,
  Shop,
  GoodsParam,
  getRecommend,
} from "network/derail.js";
// 导入防抖函数
import { debounce } from "common/utils";
// 导入混入
import { itemImageListener, isShowBackTop } from "common/mixin";
import { mapActions } from "vuex";

// import Toast from "components/common/toast/Toast";

export default {
  name: "Detail",
  components: {
    DetailNavBar,
    DetailSwiper,
    DetailBaseInfo,
    DetailShopInfo,
    DetailGoodsInfo,
    DetailGoodsParam,
    DetailCommentInfo,
    DetailBottomBar,
    scroll,
    GoodsList,
    // Toast,
  },
  data() {
    return {
      iid: null,
      topImages: [],
      goods: {},
      shop: {},
      detailInfo: {},
      goodsParam: {},
      commentInfo: {},
      recommend: [],
      themeTopYs: [],
      getThemeTopY: null,
      currentIndex: 0,
      // message: "",
      // isShow: false,
    };
  },
  mixins: [itemImageListener, isShowBackTop],
  created() {
    // 1.保存存入的iid
    this.iid = this.$route.params.iid;
    // 2.根据iid请求详细的数据
    getDetail(this.iid).then((res) => {
      // console.log(res);
      const data = res.result;
      // 1.获取顶部的图片轮播数据
      this.topImages = data.itemInfo.topImages;

      // 2.获取商品信息
      this.goods = new Goods(
        data.itemInfo,
        data.columns,
        data.shopInfo.services
      );
      // 3.创建店铺信息的对象
      this.shop = new Shop(data.shopInfo);

      // 4.保存商品的详情数据
      this.detailInfo = data.detailInfo;

      // 5.保存商品参数的数据
      this.goodsParam = new GoodsParam(
        data.itemParams.info,
        data.itemParams.rule
      );
      // 6.取出评论的信息
      if (data.rate.cRate != 0) {
        this.commentInfo = data.rate.list[0];
      }
    });
    // 3.请求推荐数据
    getRecommend().then((res) => {
      this.recommend = res.data.list;
    });
    // 4.给getThemeTopY赋值
    this.getThemeTopY = debounce(() => {
      this.themeTopYs.push(0);
      this.themeTopYs.push(this.$refs.params.$el.offsetTop);
      this.themeTopYs.push(this.$refs.comment.$el.offsetTop);
      this.themeTopYs.push(this.$refs.recommend.$el.offsetTop);
      this.themeTopYs.push(Number.MAX_VALUE);
      // console.log(this.themeTopYs);
    }, 100);
  },
  destroyed() {
    this.$bus.$off("itemImgLoad", this.itemImageListener);
  },
  methods: {
    ...mapActions({
      add: "addCart",
    }),
    imageLoad() {
      this.$refs.scroll.refresh();
      this.getThemeTopY();
    },
    titleClick(index) {
      this.$refs.scroll.scrollTo(0, -this.themeTopYs[index], 500);
    },
    scroll(position) {
      const y = -position.y;
      let length = this.themeTopYs.length;
      for (let i = 0; i < length - 1; i++) {
        // 条件一：防止赋值过于频繁
        // 条件二：(i < length - 1 && y >= this.themeTopYs[i] && y <= this.themeTopYs[i + 1]) || (i === length - 1 && y >= this.themeTopYs[i])
        // 条件1：(i < length - 1 && y >= this.themeTopYs[i] && y <= this.themeTopYs[i + 1])
        // 判断区间 i < length - 1
        // 条件2：(i === length - 1 && y >= this.themeTopYs[i])
        // 判断大于等于 i === length - 1

        // 普通做法
        // if (
        //   this.currentIndex !== i &&
        //   ((i < length - 1 &&
        //     y >= this.themeTopYs[i] &&
        //     y <= this.themeTopYs[i + 1]) ||
        //     (i === length - 1 && y >= this.themeTopYs[i]))
        // ) {
        //   this.currentIndex = i;
        //   this.$refs.navbar.currentIndex = this.currentIndex;
        // }

        // hack做法
        if (
          this.currentIndex !== i &&
          i < length - 1 &&
          y >= this.themeTopYs[i] &&
          y < this.themeTopYs[i + 1]
        ) {
          this.currentIndex = i;
          this.$refs.navbar.currentIndex = this.currentIndex;
          // console.log(this.currentIndex);
        }
      }
      this.ShowBackTop(position);
    },
    addCart() {
      // 获取购物车需要展示的商品的数据
      const product = {};
      product.image = this.topImages[0];
      product.title = this.goods.title;
      product.desc = this.goods.desc;
      product.price = this.goods.realPrice;
      product.iid = this.iid;
      // 将商品添加到购物车里面
      // this.$store.commit('addCart',product)
      // this.$store.dispatch("addCart", product).then(res => {
      //   console.log(res);
      // });
      this.add(product).then((res) => {
        // console.log(res);
        // this.message = res;
        // this.isShow = true;
        /* 
          下面代码没有达到预期效果的原因是关于this指向问题
          下面的this指向window，可是window下面并没有message和isShow所以不起效果
          解决方法：换成箭头函数，箭头函数没有this
        */
        /* setTimeout(function () {
          this.isShow = false;
          this.message = "";
          console.log(1);
          console.log(this);
        }, 1500); */
        // setTimeout(() => {
        //   this.isShow = false;
        //   this.message = "";
        //   // console.log(this);
        // }, 1500);
        this.$toast.show(res);
      });
    },
  },
};
</script>

<style scoped="scoped">
#detail {
  position: relative;
  height: 100vh;
  background-color: #ffffff;
  z-index: 999;
}
.detail-scroll {
  height: calc(100% - 44px - 58px);
  overflow: hidden;
}
.content {
  /* 43 49 58 */
  height: 100%;
}
.detail-nav {
  position: relative;
  background-color: #ffffff;
  z-index: 9;
}
.backTop {
  right: 12px;
  bottom: 70px;
}
</style>
